home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / communic / pcmail / main / kio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  4.9 KB  |  243 lines

  1. /*++
  2. /* NAME
  3. /*    kio 3
  4. /* SUMMARY
  5. /*    interface between uucico and k-protocol driver
  6. /* PACKAGE
  7. /*    uucp on the TUEnet
  8. /* SYNOPSIS
  9. /*    kturnon()
  10. /*
  11. /*    kwrmsg(type,str,fn)
  12. /*    char type, *str;
  13. /*    int fn;
  14. /*
  15. /*    krdmsg(str,fn)
  16. /*    char *str;
  17. /*    int fn;
  18. /*
  19. /*    krddata(fn,fp)
  20. /*    int fn;
  21. /*    FILE *fp;
  22. /*
  23. /*    kwrdata(fp,fn)
  24. /*    FILE *fp;
  25. /*    int fn;
  26. /*
  27. /*    kturnoff()
  28. /* IMPLICIT INPUTS
  29. /*    Ifn, Ofn, file descriptors
  30. /*    Debug, debugging level
  31. /* DESCRIPTION
  32. /*    The k protocol has been developed for the Sytek Localnet local area 
  33. /*    network at the Eindhoven University of Technology (THE).
  34. /*    Main features of this network are:
  35. /*
  36. /* .IP    o 
  37. /*    Network partners may talk at different baudrates. This implies
  38. /*    that the network does some buffering and that it needs flow control.
  39. /* .IP    o 
  40. /*    The network is normally not transparent for some control 
  41. /*    characters (XON,XOFF and locally-defined others), independent 
  42. /*    of the value of the eigth bit.
  43. /* .IP    o 
  44. /*    Some network stations are connected to telephone modems.
  45. /*
  46. /*    For these reasons, the k protocol must (i) rely on XON/XOFF flow 
  47. /*    control, (ii) be suitable for 7-bit data paths, (iii) avoid
  48. /*    sending of control characters and (iv) provide reliable operation
  49. /*    over dial-in and dial-out telephone lines.
  50. /*
  51. /*    Data are sent as checksummed 512-byte packets, terminated by an 
  52. /*    ASCII CR. Except for packet headers (^P), the k protocol only uses 
  53. /*    ASCII codes 040 through 0137. Three data bytes are expanded to four 
  54. /*    bytes upon transmission.
  55. /*    
  56. /*    The functions in kio.c form the interface between the uucico
  57. /*    controlling functions, and the k-protocol packet driver.
  58. /*
  59. /*    kturnon() sets the terminal line characteristics (XON/XOFF). Always
  60. /*    returns zero status.
  61. /*
  62. /*    krdmsg(), kwrmsg() exchange null-terminated strings.
  63. /*    Exit status zero, or FAIL.
  64. /*
  65. /*    krddata(), kwrdata() perform file i/o, and accounting. Exit status 
  66. /*    zero or FAIL.
  67. /*
  68. /*    kturnoff() sends a protocol abort sequence. Always returns zero 
  69. /*    status.
  70. /* FUNCTIONS AND MACROS
  71. /*    kread, kread, kwrite, kclose, k-protocol presentation layer 
  72. /* AUTHOR(S)
  73. /*    Wietse Venema
  74. /*    Eindhoven University of Technology
  75. /*    Department of Mathematics and Computer Science
  76. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  77. /* CREATION DATE
  78. /*    Mon Feb  3 10:13:34 MET 1986
  79. /* LAST MODIFICATION
  80. /*    90/01/22 13:01:54
  81. /* VERSION/RELEASE
  82. /*    2.1
  83. /*--*/
  84.  
  85. #include "uucp.h"
  86.  
  87. #define    BUFLEN    BUFSIZ
  88.  
  89. kturnon()
  90. {
  91.     kopen(Ifn);
  92.     return 0;
  93. }
  94.  
  95.  
  96. kturnoff()
  97. {
  98.     kclose(Ofn);
  99.     return 0;
  100. }
  101.  
  102.  
  103. kwrmsg(type,str,fn)
  104. char type,*str;
  105. int fn;
  106. {
  107.     char bufr[BUFSIZ],*s;
  108.  
  109.     bufr[0] = type;
  110.     s = &bufr[1];
  111.     while (*str)
  112.     *s++ = *str++;
  113.     *s = '\0';
  114.     if (*(--s) == '\n')
  115.     *s = '\0';
  116.     DEBUG(6," kwrmsg: \"%s\"\n",bufr);
  117.  
  118.     return (kwrite(fn,bufr,strlen(bufr)+1) > 0 ? 0 : FAIL);
  119. }
  120.  
  121.  
  122. krdmsg(str,fn)
  123. char *str;
  124. int fn;
  125. {
  126.     int len;
  127.  
  128.     for (;;) {
  129.     if ((len = kread(fn,str,BUFSIZ)) == 0) {
  130.         continue;
  131.     } else if (len > 0) {
  132.         str[len] = 0;
  133.         DEBUG(6," krdmsg: \"%s\"\n",str);
  134.         str += len;
  135.         if (str[-1] == '\0')
  136.         return 0;
  137.     } else {
  138.         return FAIL;
  139.     }
  140.     }
  141. }
  142.  
  143.  
  144. kwrdata(fp1,fn)
  145. FILE *fp1;
  146. {
  147.     char bufr[BUFLEN];
  148.     int len;
  149.     int ret;
  150.     time_t t1,t2;
  151.     long bytes;
  152.     char text[BUFSIZ];
  153.  
  154.     ret = FAIL;
  155.     bytes = 0L;
  156.     time(&t1);
  157.     while ((len = fread(bufr,sizeof (char),BUFLEN,fp1)) > 0) {
  158.     bytes += len;
  159.     if (kwrblk(bufr,len,fn) != len)
  160.         goto acct;
  161.     if (len != BUFLEN)
  162.         break;
  163.     }
  164.     ret = kwrblk(bufr,0,fn);
  165. acct:
  166.     time(&t2);
  167.     sprintf(text,ret == 0 ?
  168.     "sent data %ld bytes %ld secs" :
  169.     "send failed after %ld bytes",
  170.     bytes,t2 - t1);
  171.     DEBUG(1,"%s\n",text);
  172.     syslog(text);
  173.     sysacct(bytes,t2 - t1);
  174.     if (ret)
  175.     sysaccf(NULL);        /* force accounting */
  176.     return ret;
  177. }
  178.  
  179.  
  180. krddata(fn,fp2)
  181. FILE *fp2;
  182. {
  183.     int len,ret;
  184.     char bufr[BUFLEN];
  185.     time_t t1,t2;
  186.     long bytes;
  187.     char text[BUFSIZ];
  188.  
  189.     ret = FAIL;
  190.     bytes = 0L;
  191.     time(&t1);
  192.     for (;;) {
  193.     len = krdblk(bufr,BUFLEN,fn);
  194.     if (len < 0)
  195.         goto acct;
  196.     bytes += len;
  197.     if (fwrite(bufr,sizeof (char),len,fp2) != len)
  198.         goto acct;
  199.     if (len < BUFLEN)
  200.         break;
  201.     }
  202.     ret = 0;
  203. acct:
  204.     time(&t2);
  205.     sprintf(text,ret == 0 ? 
  206.     "received data %ld bytes %ld secs" :
  207.     "receive failed after %ld bytes",
  208.     bytes,t2 - t1);
  209.     DEBUG(1,"%s\n",text);
  210.     syslog(text);
  211.     sysacct(bytes,t2 - t1);
  212.     if (ret)
  213.     sysaccf(NULL);        /* force accounting */
  214.     return ret;
  215. }
  216.  
  217.  
  218. krdblk(blk,len, fn)
  219. char *blk;
  220. int len,fn;
  221. {
  222.     int i,ret;
  223.  
  224.     for (i = 0; i < len; i += ret) {
  225.     if ((ret = kread(fn,blk,len-i)) == 0) {
  226.         break;
  227.     } else if (ret > 0) {
  228.         blk += ret;
  229.     } else {
  230.         return FAIL;
  231.     }
  232.     }
  233.     return i;
  234. }
  235.  
  236.  
  237. kwrblk(blk,len,fn)
  238. char *blk;
  239. int len,fn;
  240. {
  241.     return kwrite(fn,blk,len);
  242. }
  243.